package cn.wolfcode.trip.admin.web.controller;

import cn.wolfcode.trip.admin.util.UploadUtil;
import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.domain.StrategyDetail;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IRegionService;
import cn.wolfcode.trip.base.service.IStrategyDetailService;
import cn.wolfcode.trip.base.service.IStrategyService;
import cn.wolfcode.trip.base.util.JSONResult;
import cn.wolfcode.trip.base.util.Qiniu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;

@Controller
@RequestMapping("/strategyDetail")
public class StrategyDetailController {

    @Autowired
    private IStrategyDetailService detailService;
    @Autowired
    private IStrategyService strategyService;

    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") QueryObject qo, Model model) {
        model.addAttribute("result",detailService.query(qo));
        model.addAttribute("strategies",strategyService.list());
        return "strategyDetail/list";
    }


    @RequestMapping("/saveOrUpdate")@ResponseBody
    public Object saveOrUpdate(StrategyDetail detail, MultipartFile image){

        JSONResult json = new JSONResult();
        try {
            if (image != null) {
//                String upload = UploadUtil.upload(image, UploadUtil.IMG_BASE_PATH + "/upload");
                String upload = Qiniu.upLoadImage(image.getBytes());
                detail.setCoverUrl(upload);
            }
            detailService.saveOrUpdate(detail);
        } catch (Exception e) {
            e.printStackTrace();
            json.mark(e.getMessage());
        }
        return json;
    }

    @ResponseBody@RequestMapping("/getContentByDetailId")
    public Object getContentByDetailId(Long detailId){
       return detailService.getContentByDetailId(detailId);
    }

}
